package Andrew.SpringDemo.Controller;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Andrew.SpringDemo.Model.User;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/User")
public class UserController {


    private static final long serialVersionUID = 1L;

    private String serverPath = "D:/";

    @RequestMapping(value = "/doUpload",method = RequestMethod.POST)
    public void doUpload(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.getWriter().append("Served at: ").append(request.getContextPath());

        System.out.println("进入后台...");

        // 1.创建DiskFileItemFactory对象，配置缓存用
        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();

        // 2. 创建 ServletFileUpload对象
        ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);

        // 3. 设置文件名称编码
        servletFileUpload.setHeaderEncoding("utf-8");

        // 4. 开始解析文件
        try {
            List<FileItem> items = servletFileUpload.parseRequest(request);
            for (FileItem fileItem : items) {

                if (fileItem.isFormField()) { // >> 普通数据
                    String info = fileItem.getString("utf-8");
                    System.out.println("info:" + info);
                } else { // >> 文件
                    // 1. 获取文件名称
                    String name = fileItem.getName();
                    // 2. 获取文件的实际内容
                    InputStream is = fileItem.getInputStream();

                    // 3. 保存文件
                    FileUtils.copyInputStreamToFile(is, new File(serverPath + "/" + name));
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }




        /*
     * @Autowired UserService server;
     */
    @RequestMapping(value = "/PreView", method = RequestMethod.GET)
    public String PreView(Model model, String path) {
        String finalPath=path;
        model.addAttribute("path", finalPath);
        return "PreView";
    }

    /*
     * @Autowired UserService server;
     */

    @RequestMapping(value = "/List", method = RequestMethod.GET)
    public String List(Model model) {

        ArrayList<User> list = new ArrayList<User>();
        for (int i = 0; i < 12; i++) {
            User user = new User();
            user.Id = 0;
            user.Name = "wmg";
            user.Age = 28;
            list.add(user);
        }

        model.addAttribute("tableData", list);
        return "List";
    }
}
